﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// DataChooser.xaml 的交互逻辑
    /// </summary>
    public partial class DataChooser : DockPanel
    {
        public DataChooser()
        {
            InitializeComponent(); 
        }
        protected override void OnVisualParentChanged(DependencyObject oldParent)
        {
            base.OnVisualParentChanged(oldParent);
            if (oldParent != null)
            {
                if (oldParent is FrameworkElement fe1)
                {
                    fe1.DataContextChanged -= Fe_DataContextChanged;
                }
            }
            if (this.Parent is FrameworkElement fe)
            {
                this.DataContext = fe.DataContext;
                fe.DataContextChanged += Fe_DataContextChanged;
            }
            else
            {
                this.DataContext = null;
            }
        }



        private void Fe_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            this.DataContext = e.NewValue;
        }

        private void btn_search_Click(object sender, RoutedEventArgs e)
        {
            SearchClick?.Invoke(this, EventArgs.Empty);
        }

        public event EventHandler SearchClick;
         


        public string DisplayText
        {
            get { return (string)GetValue(DisplayTextProperty); }
            set { SetValue(DisplayTextProperty, value); }
        }
        public static readonly DependencyProperty DisplayTextProperty =
            DependencyProperty.Register("DisplayText", typeof(string), typeof(DataChooser), new PropertyMetadata(null), obj => { return true; });
         
        public object Value
        {
            get { return (object)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
         
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(object), typeof(DataChooser), new PropertyMetadata(null));




        public string WaterMark
        {
            get { return (string)GetValue(WaterMarkProperty); }
            set { SetValue(WaterMarkProperty, value); }
        }
         
        public static readonly DependencyProperty WaterMarkProperty =
            DependencyProperty.Register("WaterMark", typeof(string), typeof(DataChooser), new PropertyMetadata("点击右侧按钮选择一条数据"));



        public ImageSource ButtonIcon
        {
            get { return (ImageSource)GetValue(ButtonIconProperty); }
            set { SetValue(ButtonIconProperty, value); }
        }
         
        public static readonly DependencyProperty ButtonIconProperty =
            DependencyProperty.Register("ButtonIcon", typeof(ImageSource), typeof(DataChooser), 
                new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/SharpSoft.WPF;component/res/query.png")))
                );



        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        { 
            base.OnPropertyChanged(e);
        }



    }
}
